//
//  QuotesAndOrder.m
//  HappyStore
//
//  Created by 李小辉 on 16/11/24.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "QuotesAndOrder.h"
#import "SupplyChainVC.h"
#import "QuotesAndOrderCell.h"
@interface QuotesAndOrder ()<UITableViewDelegate, UITableViewDataSource>
@property (assign, nonatomic) NSInteger type;//收到or提交
@property (assign, nonatomic) NSInteger page;
@property (strong, nonatomic) NSMutableArray *dataAry;//数据源;
@property (strong, nonatomic) UITableView *tabelView;
@end

static NSString *quotesAndOrderCell = @"quotesAndOrderCell";
@implementation QuotesAndOrder


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //导航条都透明度
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.f],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //导航条都透明度
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *segmentedAry = @[@"收到",@"提交"];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedAry];
    segmentedControl.frame = CGRectMake(0, 0, 120, 25);
    UIFont *font = [UIFont systemFontOfSize:14];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    [segmentedControl setTitleTextAttributes:dic forState:UIControlStateNormal];
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor =  RGB(55, 195, 228);
    [segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedControl;
    
    self.view.backgroundColor = RGB(247, 247, 247);
    
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.page = 1;
    self.tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    //去掉自定义cell底部的黑线
    self.tabelView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    [self.view addSubview:self.tabelView];
    // 集成下拉刷新
    self.tabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.dataAry.count != 0) {
            if ([[USER_DEFAULTS objectForKey:@"isOpenSound"] boolValue]) {//声音开关
                NSString *path = [[NSBundle mainBundle]pathForResource:@"refresh"
                                                                ofType:@"wav"];
                
                SystemSoundID soundID;
                AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
                
                AudioServicesPlaySystemSound(soundID); //仅播放音频
            }
            self.page = 1;
            [self startNetWork];
        }else
            [self.tabelView.mj_header endRefreshing];
    }];
    
    // 集成上拉加载更多
    self.tabelView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [self startNetWork];
    }];
    self.tabelView.mj_footer.hidden = YES;
    //注册
    UINib *nib = [UINib nibWithNibName:@"QuotesAndOrderCell" bundle:nil];
    [self.tabelView registerNib:nib forCellReuseIdentifier:quotesAndOrderCell];
    self.type = 1;
    [self startNetWork];
}
//返回
- (void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Segmented
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)control{
    if (control.selectedSegmentIndex == 0) {
        self.type = 1;
        [self startNetWork];
    }else{
        self.type = 2;
        [self startNetWork];
    }
}

#pragma mark - tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:self.dataAry.count];
    return self.dataAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QuotesAndOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:quotesAndOrderCell];
    
    NSDictionary *dic = self.dataAry[indexPath.row];
    [cell.headPhoto sd_setImageWithURL:[NSURL URLWithString:dic[@"headPhoto"]] placeholderImage:[UIImage imageNamed:@"登录-logo"]];
    cell.titleLabel.text = dic[@"title"];
    if ([dic[@"userName"] isEqualToString:@"null"]) {
        cell.userNameLabel.text = @"";
    }else{
        cell.userNameLabel.text = dic[@"userName"];
    }
    if ([dic[@"status"] integerValue] == 0) {
        cell.statusLabel.text = @"未查看";
    }else if ([dic[@"authState"] integerValue] == 1) {
        cell.statusLabel.text = @"处理";
    }else if ([dic[@"authState"] integerValue] == 2) {
        cell.statusLabel.text = @"待沟通";
    }else if ([dic[@"authState"] integerValue] == 3) {
        cell.statusLabel.text = @"不合适";
    }else{
        cell.statusLabel.text = @"成交";
    }
    
    if ([self.typeStr isEqualToString:@"在线报价"]) {
        cell.moneyNumLabel.text = [CustomExcetion comperNumber:dic[@"offerMoney"]];
    }else{
        cell.moneyNumLabel.text = dic[@"offerNumber"];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.typeStr isEqualToString:@"在线报价"]) {
        SupplyChainVC *supplyChainVc = [[SupplyChainVC alloc] init];
        supplyChainVc.tagStr = @"我的";
        supplyChainVc.typeId = 2;
        supplyChainVc.resId = self.dataAry[indexPath.row][@"id"];
        [self.navigationController pushViewController:supplyChainVc animated:YES];
    }else{//在线订购
        SupplyChainVC *supplyChainVc = [[SupplyChainVC alloc] init];
        supplyChainVc.tagStr = @"我的";
        supplyChainVc.typeId = 1;
        supplyChainVc.resId = self.dataAry[indexPath.row][@"id"];
        [self.navigationController pushViewController:supplyChainVc animated:YES];
    }
    
}


#pragma mark - 网络请求
- (void)startNetWork{
    
    NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
    muDic[@"page"] = @(self.page);
    muDic[@"userId"] = [USER_DEFAULTS objectForKey:@"userid"];
    muDic[@"token"] = [USER_DEFAULTS objectForKey:@"token"];
    NSString *urlStr = @"";
    if ([self.typeStr isEqualToString:@"在线报价"]) {
        if (self.type == 1) {//收到
            urlStr = @"getOfferShou_app_chain.im.lensum";
        }else{//提交
            urlStr = @"getOfferTi_app_chain.im.lensum";
        }
    }else{//在线订购
        if (self.type == 1) {//收到
            urlStr = @"getOrderShou_app_chain.im.lensum";
        }else{//提交
            urlStr = @"getOrderTi_app_chain.im.lensum";
        }
    }
    
    [LSHttpTool sendHttpData:urlStr params:muDic success:^(id responseObject) {
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        if ([dicInfo[@"result"] integerValue] == 1) {
            
            if (self.page == 1) {
                [self.tabelView.mj_header endRefreshing];
                self.dataAry = [NSMutableArray arrayWithArray:dicInfo[@"data"]];
                if (self.dataAry.count == 10 ) {
                    self.tabelView.mj_footer.hidden = NO;
                }else{
                    self.tabelView.mj_footer.hidden = YES;
                }
            }else{
                [self.tabelView.mj_footer endRefreshing];
                NSArray *aryIn = [[NSArray alloc] init];
                aryIn = dicInfo[@"data"];
                for (int i = 0; i < aryIn.count; i++) {
                    NSDictionary *dic = aryIn[i];
                    [self.dataAry addObject:dic];
                }
                if (aryIn.count == 10) {
                    self.tabelView.mj_footer.hidden = NO;
                }else{
                    self.tabelView.mj_footer.hidden = YES;
                }
            }
            [self.tabelView reloadData];
        }else{

        }
        
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"未搜索到资源"];
    }];
    
}


@end
